from pprint import pprint

from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View
from apps.goods.models import SPU, SKU, GoodsCategory, SKUImage
from apps.goods.utils import get_breadcrumb
from apps.orders.models import OrderGoods
from utils.response_code import RETCODE


class Goods_Paint_View(View):
    """全部商品页面"""

    def get(self, request):
        return render(request, 'paint.html')


class FlowerDer_View(View):
    """装饰摆件页面"""

    def get(self, request):
        spu1 = SKU.objects.filter(spu=3).all()
        spu2 = SKU.objects.filter(spu=4).all()
        sku = SKU.objects.filter(spu = 5).all()
        sku6 = SKU.objects.filter(spu=6).all()

        content = {
            'skus': spu1,
            'sku': sku,
            'sku5': spu2,
            'sku6': sku6,
        }
        return render(request, "flowerDer.html", content)


class ProList_View(View):
    """干花花艺"""

    def get(self, request):
        spu = SKU.objects.filter(spu=3).all()
        sku = SKU.objects.filter(spu=5).all()
        content = {
            'skus': spu,
            'sku': sku
        }
        return render(request, "proList.html", content)


class Vase_ProList_View(View):
    """花瓶花器"""

    def get(self, request):
        spu = SKU.objects.filter(spu=4).all()
        spu6 = SKU.objects.filter(spu=6).all()

        content = {
            'skus': spu,
            'sku6': spu6,

        }

        return render(request, "vase_proList.html", content)


class Decoration_View(View):
    """布艺软式"""

    def get(self, request):

        return render(request, "decoration.html")


class ZbproList_View(View):
    """桌布页面"""

    def get(self, request):

        spu = SKU.objects.filter(spu=2).all()

        content = {
            'skus': spu,

        }

        return render(request, "zbproList.html", content)


class BzproList_View(View):
    """抱枕"""

    def get(self, request):

        spu = SKU.objects.filter(spu=1).all()

        content = {
            'skus': spu,

        }

        return render(request, "bzproList.html", content)


class PerfunmeView(View):
    """蜡艺香薰"""
    def get(self, request):

        return render(request, "perfume.html")

class IdeaView(View):
    """创意家居"""
    def get(self, request):
        return render(request, "idea.html")

class DetailView(View):
    """商品详情页"""
    def get(self, request, sku_id):
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return render(request, '404.html')

        image = SKUImage.objects.filter(sku=sku)
        print(sku_id, '======================')
        order_goods_list = OrderGoods.objects.filter(sku_id=sku_id, is_commented=True)[:30]
        print(order_goods_list, '2222222222222222')
        # 序列化
        comment_list = []
        for order_goods in order_goods_list:
            username = order_goods.order.user.username
            comment_list.append({
                'username': username + '***' + username[-1] if order_goods.is_anonymous else username,
                'comment': order_goods.comment,
                'score': order_goods.score,
                'image': order_goods.order.user.image
            })
        print(comment_list, '111111111111111111111')
        content = {
            'sku': sku,
            'image': image,
            'sku_id': sku_id,
            'comment': comment_list
        }

        return render(request, 'proDetail.html', content)


class GoodsCommentView(View):
    """商品评价信息"""
    # 获取被评价的订单商品信息
    def get(self, request, sku_id):
        order_goods_list = OrderGoods.objects.filter(sku_id=sku_id, is_commented=True)[:30]
        # 序列化
        comment_list = []
        for order_goods in order_goods_list:
            username = order_goods.order.user.username
            comment_list.append({
                'username': username + '***' + username[-1] if order_goods.is_anonymous else username,
                'comment': order_goods.comment,
                'score': order_goods.score,
            })
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'comment_list': comment_list})